<?php
	require('jacked_configVars.php');
	require('jacked_functions.php');		
	if(!validateLogin()){
		exit();
	}
	require('jacked_configpdnet.php');
	require('jacked_pdnetfunctions.php');
	
	$pageType = "jacked";
	$pageTitle = "JACKED Manager";
		
	require('bodyTop.php');
	echo JACKED_HEADING_STRING;

    

?>	

   	<h2>JACKED PoorDecisions.net</h2>
   	
   	<?php echo $msg; ?>
   	
   	<?php 
    //$db_selected = mysql_select_db('pdnet_content', $link);
//if (!$db_selected){
  //  die('Cant use pd : ' . mysql_error());
   // }

$thedecision=$_POST['thedecision'];
$theresult=$_POST['theresult'];
$theid=$_POST['theid'];
$thetags=$_POST['thetags'];

        $query="INSERT INTO PoorDecision (`decision`, `result`, `votes`, `alive`) VALUES ('".mysql_real_escape_string($thedecision)."', '".mysql_real_escape_string($theresult)."', '0', '0')";
        $result=mysql_query($query, JACKED_DEFAULT_LINK);
        $pid=mysql_insert_id();
        $qquery="INSERT INTO Queue (`PoorDecision`) VALUES ('".$pid."')";
        $qresult=mysql_query($qquery, JACKED_DEFAULT_LINK);
        echo "Poor Decision ".$pid." has been added <br>";
        if ($result){
            #echo $result;
            }
            else {
                echo mysql_error();
            }
            $query4="UPDATE Submission SET `postID` = '".$pid."', `posted`= 1  WHERE id='".$theid."'";
            echo $query4;
            $result4=mysql_query($query4, JACKED_DEFAULT_LINK);
            
foreach(explode(",", $thetags) as $tag){
                $query="SELECT id FROM Tag where name='".mysql_real_escape_string($tag)."'";
             #   echo $query;
                echo "<br />";
                $result=mysql_query($query, JACKED_DEFAULT_LINK);
                $num=mysql_numrows($result);
             #   echo $num;
           #     echo "WHYYYYY DREW";
                if ($num == 0){
                    $query="INSERT INTO Tag (`usage`, `name`, `slug`) VALUES ('1', '".mysql_real_escape_string($tag)."','".slugify($tag)."')";
                    $result=mysql_query($query, JACKED_DEFAULT_LINK);
                    $tid=mysql_insert_id();
                    echo "Tag  id number ".$tid." added";
                    $query2="INSERT INTO TagRel VALUES ('".$pid."','".$tid."')";
                    $result2=mysql_query($query2, JACKED_DEFAULT_LINK);
                    $result=NULL;
                    if ($result2){
                        #echo $result2;
                        }
                        else {
                            echo mysql_error();
                        }
            //echo $tag;
            //echo "<br />";
            }else{
                        $trow=mysql_fetch_array($result);
                        $tid=$trow[0];
                        $query2="INSERT INTO TagRel VALUES ('".$pid."','".$tid."')";
                        $result2=mysql_query($query2, JACKED_DEFAULT_LINK);
                        $result=NULL;
                        $query3="UPDATE Tag SET `usage`=`usage`+1 WHERE id='".$tid."'";
                        $result3=mysql_query($query3, JACKED_DEFAULT_LINK);
                     //   $query4="UPDATE Submission SET `postID` = '".$pid."', `posted`= 1  WHERE id='".$theid."'";
                       // echo $query4;
                       // $result4=mysql_query($query4);
                        if  ($result2){
                            #echo $result2;
                        }
                        else {
                            echo mysql_error();
                        }
            }
    
}
?>
<div class="blogspace"></div>
<a href="jacked_admin.php">Cancel/Back</a>


<?php	
	require('bodyBottom.php');
?>